﻿IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'P_GetUserMenus')
	BEGIN
		DROP  Procedure  [esf_sso].P_GetUserMenus
	END

GO


CREATE Procedure [esf_sso].P_GetUserMenus
	@pApplicationID	INT = NULL
	,@pUserGroupID	INT = NULL
	,@pParentUserMenuID	INT = NULL
AS

	SELECT
		DISTINCT 
		VU.ChiBR_ID				ID
		,VU.ChiBR_Code			Code
		,VU.ChiBR_Name			Name
		,VU.ChiBR_Description	Description
		,VU.URL
		,VU.ChiBR_Extra_I
		,VU.ParBR_Code			ParentCode
		,VU.ParBR_ID			ParentID
		,VU.ChiBR_IsEnabled		IsEnabled
		-- UserMenu , UserGroup (parent)
		,(SELECT TOP 1 1 FROM [esf_sso].TreeBasicReference UG_UM 
			WHERE 
				BasicReferenceID = VU.ChiBR_ID 
				AND ParentBasicReferenceID = UG_UM.ParentBasicReferenceID
				AND (@pUserGroupID IS NULL OR UG_UM.ParentBasicReferenceID = @pUserGroupID)) calMarked
	FROM 
		[esf_sso].VUserMenus VU
	
	WHERE 
		VU.Application_ID = @pApplicationID
		AND (@pParentUserMenuID IS NULL AND VU.ParBR_ID IS NULL
			OR VU.ParBR_ID = @pParentUserMenuID)
			
GO